2 * Add search suggestions to the search form.
6 var api
, map
, searchboxesSelectors
,
7 // Region where the suggestions box will appear directly below
8 // (using the same width). Can be a container element or the input
9 // itself, depending on what suits best in the environment.
10 // For Vector the suggestion box should align with the simpleSearch
11 // container's borders, in other skins it should align with the input
12 // element (not the search form, as that would leave the buttons
13 // vertically between the input and the suggestions).
14 $searchRegion
= $( '#simpleSearch, #searchInput' ).first(),
15 $searchInput
= $( '#searchInput' ),
16 previousSearchText
= $searchInput
.val();
20 // SimpleSearch is broken in Opera < 9.6
22 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
23 konqueror
: [['>=', '4.11']],
26 // Support for iOS 6 or higher. It has not been tested on iOS 5 or lower
31 if ( !$.client
.test( map
) ) {
35 // Compute form data for search suggestions functionality.
36 function getFormData( context
) {
37 var $form
, baseHref
, linkParams
;
39 if ( !context
.formData
) {
40 // Compute common parameters for links' hrefs
41 $form
= context
.config
.$region
.closest( 'form' );
43 baseHref
= $form
.attr( 'action' );
44 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
46 linkParams
= $form
.serializeObject();
49 textParam
: context
.data
.$textbox
.attr( 'name' ),
50 linkParams
: linkParams
,
55 return context
.formData
;
59 * Callback that's run when the user changes the search input text
60 * 'this' is the search input box (jQuery object)
63 function onBeforeUpdate() {
64 var searchText
= this.val();
66 if ( searchText
&& searchText
!== previousSearchText
) {
67 mw
.track( 'mediawiki.searchSuggest', {
68 action
: 'session-start'
71 previousSearchText
= searchText
;
75 * Callback that's run when suggestions have been updated either from the cache or the API
76 * 'this' is the search input box (jQuery object)
79 function onAfterUpdate() {
80 var context
= this.data( 'suggestionsContext' );
82 mw
.track( 'mediawiki.searchSuggest', {
83 action
: 'impression-results',
84 numberOfResults
: context
.config
.suggestions
.length
,
85 // FIXME: when other types of search become available change this value accordingly
86 // See the API call below (opensearch = prefix)
87 resultSetType
: 'prefix'
91 // The function used to render the suggestions.
92 function renderFunction( text
, context
) {
93 var formData
= getFormData( context
);
95 // linkParams object is modified and reused
96 formData
.linkParams
[ formData
.textParam
] = text
;
98 // this is the container <div>, jQueryfied
102 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
103 .attr( 'title', text
)
104 .addClass( 'mw-searchSuggest-link' )
108 // The function used when the user makes a selection
109 function selectFunction( $input
) {
110 var context
= $input
.data( 'suggestionsContext' ),
113 mw
.track( 'mediawiki.searchSuggest', {
114 action
: 'click-result',
115 numberOfResults
: context
.config
.suggestions
.length
,
116 clickIndex
: context
.config
.suggestions
.indexOf( text
) + 1
119 // allow the form to be submitted
123 function specialRenderFunction( query
, context
) {
125 formData
= getFormData( context
);
127 // linkParams object is modified and reused
128 formData
.linkParams
[ formData
.textParam
] = query
;
130 if ( $el
.children().length
=== 0 ) {
134 .addClass( 'special-label' )
135 .text( mw
.msg( 'searchsuggest-containing' ) ),
137 .addClass( 'special-query' )
142 $el
.find( '.special-query' )
146 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
147 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
151 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
152 .addClass( 'mw-searchSuggest-link' )
157 // Generic suggestions functionality for all search boxes
158 searchboxesSelectors
= [
159 // Primary searchbox on every page in standard skins
161 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
162 // and for MediaWiki itself (special pages with page title inputs)
165 $( searchboxesSelectors
.join( ', ' ) )
167 fetch: function ( query
, response
, maxRows
) {
170 api
= api
|| new mw
.Api();
172 $.data( node
, 'request', api
.get( {
173 action
: 'opensearch',
178 } ).done( function ( data
) {
179 response( data
[ 1 ] );
182 cancel: function () {
184 request
= $.data( node
, 'request' );
188 $.removeData( node
, 'request' );
192 render
: renderFunction
,
193 select: function () {
194 // allow the form to be submitted
201 .bind( 'paste cut drop', function () {
202 // make sure paste and cut events from the mouse and drag&drop events
203 // trigger the keypress handler and cause the suggestions to update
204 $( this ).trigger( 'keypress' );
206 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
207 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
208 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
210 var $this = $( this );
212 .data( 'suggestions-context' )
214 .css( 'fontSize', $this.css( 'fontSize' ) );
217 // Ensure that the thing is actually present!
218 if ( $searchRegion
.length
=== 0 ) {
219 // Don't try to set anything up if simpleSearch is disabled sitewide.
220 // The loader code loads us if the option is present, even if we're
221 // not actually enabled (anymore).
225 // Special suggestions functionality and tracking for skin-provided search box
226 $searchInput
.suggestions( {
228 before
: onBeforeUpdate
,
232 render
: renderFunction
,
233 select
: selectFunction
236 render
: specialRenderFunction
,
237 select: function ( $input
) {
238 $input
.closest( 'form' )
239 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
240 return true; // allow the form to be submitted
243 $region
: $searchRegion
246 $searchInput
.closest( 'form' )
247 // track the form submit event
248 .on( 'submit', function () {
249 var context
= $searchInput
.data( 'suggestionsContext' );
250 mw
.track( 'mediawiki.searchSuggest', {
251 action
: 'submit-form',
252 numberOfResults
: context
.config
.suggestions
.length
255 // If the form includes any fallback fulltext search buttons, remove them
256 .find( '.mw-fallbackSearchButton' ).remove();
259 }( mediaWiki
, jQuery
) );